//
//  BPTimeChooseView.m
//  BabyPhotograph
//
//  Created by cesh on 2017/3/1.
//  Copyright © 2017年 cesh. All rights reserved.
//

#import "BPTimeChooseView.h"

@interface BPTimeChooseView ()

@property (strong, nonatomic) UITapGestureRecognizer *tap;

@end

@implementation BPTimeChooseView

- (instancetype)init {
    if (self = [super init]) {
        self.tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectTimeView)];
        }
    return self;
}

- (void)setTimeChooseType:(TimeChooseType)timeChooseType {
    _timeChooseType = timeChooseType;
    switch (timeChooseType) {
        case TimeChooseTypeChooseable:
        {
            self.backgroundColor = [UIColor colorWithRed:236/255.0 green:245/255.0 blue:252/255.0 alpha:1/1.0];
            for (UIView *view in self.subviews) {
                if ([view isKindOfClass:[UILabel class]]) {
                    UILabel *label = (UILabel *)view;
                    label.textColor = [UIColor colorWithRed:141/255.0 green:165/255.0 blue:184/255.0 alpha:1/1.0];
                }
            }
            //添加选中与否的点击事件
            [self addGestureRecognizer:self.tap];
            
        }
            break;
        case TimeChooseTypeUnchooseable:
        {
            self.backgroundColor = [UIColor colorWithRed:155/255.0 green:155/255.0 blue:155/255.0 alpha:1/1.0];
            for (UIView *view in self.subviews) {
                if ([view isKindOfClass:[UILabel class]]) {
                    UILabel *label = (UILabel *)view;
                    label.textColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1/1.0];
                }
            }
            [self removeGestureRecognizer:self.tap];
        }
            break;
        case TimeChooseTypeChosen:
        {
            self.backgroundColor =  [UIColor colorWithRed:118/255.0 green:177/255.0 blue:253/255.0 alpha:1/1.0];
            for (UIView *view in self.subviews) {
                if ([view isKindOfClass:[UILabel class]]) {
                    UILabel *label = (UILabel *)view;
                    label.textColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1/1.0];
                }
            }
            [self addGestureRecognizer:self.tap];
        }
            break;
        default:
            break;
    }
}

- (void)selectTimeView {
    self.timeChooseType = (self.timeChooseType == TimeChooseTypeChooseable) ? TimeChooseTypeChosen : TimeChooseTypeChooseable;
    //如果是选中状态
    if (self.timeChooseType == TimeChooseTypeChosen) {
        for (UIView *view in self.subviews) {
            if ([view isKindOfClass:[UILabel class]]) {
                UILabel *label = (UILabel *)view;
                if ([self.delegate respondsToSelector:@selector(registerTime:time_id:)]) {
                    [self.delegate registerTime:label.text time_id:self.tag];
                }
            }
        }
    }
}

@end
